package com.service;

import com.bean.WebsiteVisitorAbnormal;
import com.bean.WebsiteVisitorDisable;
import com.mapper.mysql.WebsiteVisitorMapper;
import com.mapper.repository.mysql.WebsiteVisitorAbnormalRepository;
import com.mapper.repository.mysql.WebsiteVisitorDisableRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class WebsiteVisitorServiceImpl implements WebsiteVisitorService{

    @Autowired
    private WebsiteVisitorMapper websiteVisitorMapper;

    @Autowired
    private WebsiteVisitorAbnormalRepository websiteVisitorAbnormalRepository;

    @Override
    public void handleVisitorApiAbnormal() {
        //查询只访问了   前台页面访问 /的记录
        List<String> list = websiteVisitorMapper.listWebsiteVisitorApiByApi();
        if(list.size()>0){
            int delWebsiteVisitorApiFlag = websiteVisitorMapper.delWebsiteVisitorApiByApi(list);
            int delWebsiteVisitorFlag = websiteVisitorMapper.delWebsiteVisitorByApi(list);

            List<WebsiteVisitorAbnormal> abnormals = new ArrayList<>();
            for(String addr:list){
                WebsiteVisitorAbnormal websiteVisitorAbnormal = new WebsiteVisitorAbnormal();
                String id = UUID.randomUUID().toString();
                websiteVisitorAbnormal.setId(id);
                websiteVisitorAbnormal.setCreatetime(LocalDateTime.now());
                websiteVisitorAbnormal.setAddr(addr);

                abnormals.add(websiteVisitorAbnormal);
            }
            websiteVisitorAbnormalRepository.saveAll(abnormals);
        }

    }


    @Autowired
    private WebsiteVisitorDisableRepository websiteVisitorDisableRepository;

    @Override
    public void handleVisitorAbnormal() {
        List<String> list = websiteVisitorMapper.listWebsiteVisitorAbnormal();
        if(list.size()>0){
            int delWebsiteVisitorAbnormalFlag = websiteVisitorMapper.delWebsiteVisitorAbnormal(list);

            List<WebsiteVisitorDisable> disables = new ArrayList<>();
            for(String addr : list){
                WebsiteVisitorDisable websiteVisitorDisable = new WebsiteVisitorDisable();

                String id = UUID.randomUUID().toString();
                websiteVisitorDisable.setId(id);
                websiteVisitorDisable.setCreatetime(LocalDateTime.now());
                websiteVisitorDisable.setAddr(addr);

                disables.add(websiteVisitorDisable);

            }
            websiteVisitorDisableRepository.saveAll(disables);
        }

    }
}
